import axios from "axios";
import main from "../main.js";
import {
    Toast
} from "vant";
const httpAxios = axios.create(); //创建实例
let Config = {
    TIMEOUT: 6000, //设置超时时间为6秒
};
// axios 配置
httpAxios.defaults.timeout = Config.TIMEOUT;
httpAxios.interceptors.request.use((config) => {
    if (config.url !== "/api/course/user/login") {
        // 给请求头的设置Token
        config.headers["GLToken"] = localStorage.getItem('token') || ''
    }
    return config
}, (error) => {
    return Promise.reject(error);
});
// axios 配置
httpAxios.defaults.timeout = Config.TIMEOUT;
httpAxios.interceptors.response.use(
    (response) => {
        return response;
    },
    (error) => {
        if (error.message.includes("timeout")) {
            Toast("请求超时，请稍后再试");
            return Promise.reject(error);
        }
        Toast("网络出错，请稍后再试");
        return Promise.reject(error);
    }
);
export function request(type = "get", url = "", params = {}) {
    return new Promise((resolve, reject) => {
        let data = {
            method: type,
            // url: process.env.NODE_ENV === "development" ? url : '/xjz' + url,
            url: process.env.NODE_ENV === "development" ? url : '' + url,
            headers: {
                "Content-Type": "application/json; charset=UTF-8",
            },
        }
        if (type == "get") {
            data.params = params
        } else {
            data.data = params
        }
        httpAxios(data).then((response) => {
            if (response.data.code == 9) {
                localStorage.removeItem("userInfo");
                localStorage.removeItem("token");
                main.$router.push("/home");
            }
            resolve(response.data);
        }).catch((error) => {
            reject(error);
        });
    });
}